package cn.tedu.malls.product.webapi.controller;

import cn.tedu.mall.sproduct.service.ICategoryService;
import cn.tedu.malls.commons.response.JsonResult;
import cn.tedu.malls.pojo.dto.CategoryAddNewDTO;
import cn.tedu.malls.pojo.vo.CategoryAddNewVO;
import cn.tedu.malls.pojo.vo.CategoryListItemVO;
import cn.tedu.malls.pojo.vo.CategoryStandardVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categoris")
@Api(tags = "类别管理")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;

    @GetMapping("/list")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "查询所有类别")
    public JsonResult<List<CategoryListItemVO>> list(){
        List<CategoryListItemVO> list = categoryService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/getById{id}")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParam(value = "类别id", name = "id",required = true)
    @ApiOperation(value = "根据id查询类别")
    public JsonResult<CategoryStandardVO> getById(@PathVariable("id") Long id){
        CategoryStandardVO categoryStandardVO = categoryService.getById(id);
        return JsonResult.ok(categoryStandardVO);
    }

    @PostMapping("/addNew")
    @ApiOperation(value = "增加类别")
    @ApiOperationSupport(order = 30)
    public JsonResult<CategoryAddNewVO> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO){
        Long id = categoryService.addNew(categoryAddNewDTO);
        CategoryAddNewVO categoryAddNewVO = new CategoryAddNewVO();
        categoryAddNewVO.setId(id);
        return JsonResult.ok(categoryAddNewVO);
    }
}
